package be.destin.cayenne;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.cayenne.util.XMLEncoder;
import org.apache.cayenne.util.XMLSerializable;

public class CayenneHelpers {
	
	public static long lastError = 0;

	static final long MINWAIT = 120000; // 2 minutes...

	/*
	 * Makes an XML dump of a Cayenne Select query to ease debugging
	 */
	public static String xmlDump (XMLSerializable query) {
		if (query == null) return ("<null/>");
		StringWriter dumpXML = new StringWriter();
		PrintWriter out = new PrintWriter(dumpXML);
		XMLEncoder queryXML = new XMLEncoder(out);
		query.encodeAsXML(queryXML);
		return dumpXML.toString();
	}

	public static boolean isOK() {
		java.util.Date now = new java.util.Date();
		if (lastError != 0) {
			if ( (now.getTime() - lastError) < MINWAIT) {
				return false;
			}
			lastError = 0;
		}
		return true;
	}
	
	public static void notOK() {
		java.util.Date now = new java.util.Date();
		lastError = now.getTime();
	}

}
